package com.lagou.web;

import com.lagou.domain.Employee;
import com.lagou.service.EmployeeService;
import com.lagou.util.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询列表
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Employee> list = employeeService.findAll();
        //日期格式转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(Employee e:list){
            e.setTime(simpleDateFormat.format(e.getIntime()));
        }
        model.addAttribute("list",list);
        return "list";
    }

    /**
     * 保存
     * @param employee
     * @return
     */
    @RequestMapping("/save")
    public String save(Employee employee){
        //日期格式转换
        employee.setIntime(new DateConverter().convert(employee.getTime()));
        employeeService.save(employee);
        return "redirect:/employee/findAll";
    }
}
